﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace Site.Data
{
    public class DataTransaction
    {

        /// <summary>
        /// Classe que realiza a transações através dos comandos informados
        /// </summary>

        #region Campos

        private MySqlConnection _cnnCon;//conexão que será utilizada para a transação
        private MySqlCommand _cmdCommand;// command que será utilizado para executar a transação
        private MySqlTransaction _sqtTransaction;//guarda a transação utilizada pela conexão
        #endregion

        #region Construtores
        /// <summary>
        /// Inicializa a classe
        /// </summary>
        /// <param name="cnnConexao_">Objeto de Conexão com banco de dados</param>
        internal DataTransaction(MySqlConnection cnnConexao_)
        {
            _cnnCon = cnnConexao_;
            _cnnCon.Open();
            _sqtTransaction = _cnnCon.BeginTransaction();
            _cmdCommand = new MySqlCommand();
            _cmdCommand.Connection = _cnnCon;
            _cmdCommand.Transaction = _sqtTransaction;
        }

        /// <summary>
        /// Inicializa a classe
        /// </summary>
        /// <param name="cnnConexao_">Objeto de Conexão com banco de dados</param>
        /// <param name="enumLevel_">Isolation Level da transação</param>
        internal DataTransaction(MySqlConnection cnnConexao_, IsolationLevel enumLevel_)
        {
            _cnnCon = cnnConexao_;
            _cnnCon.Open();
            _sqtTransaction = _cnnCon.BeginTransaction(enumLevel_);
            _cmdCommand = new MySqlCommand();
            _cmdCommand.Connection = _cnnCon;
            _cmdCommand.Transaction = _sqtTransaction;
        }
        #endregion

        #region Propriedades
        /// <summary>
        /// Objeto Command
        /// </summary>
        internal MySqlCommand Command
        {
            get
            {
                return _cmdCommand;
            }
        }
        #endregion

        #region Métodos
        /// <summary>
        /// Salva as alterações no banco
        /// </summary>
        public void Commit()
        {
            _sqtTransaction.Commit();
        }

        /// <summary>
        /// Desfaz todas as operações efetuadas
        /// </summary>
        public void Rollback()
        {
            _sqtTransaction.Rollback();
        }

        /// <summary>
        /// Finaliza a transação
        /// </summary>
        public void EndTransaction()
        {
            if (_sqtTransaction != null) _sqtTransaction.Dispose();
            if (_cmdCommand != null) _cmdCommand.Dispose();
            if (_cnnCon != null && _cnnCon.State != ConnectionState.Closed)
            {
                _cnnCon.Close();
                _cnnCon.Dispose();
            }
        }
        #endregion
    }
}
